﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using weiwei.Models;

namespace weiwei.Controllers
{
    public class DefaultController : Controller
    {
        Model1 model = new Model1();
        // GET: Default
        public ActionResult Index()
        {
            var list = model.Task.ToList();
            return View(list);
        }
        //添加
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(Task task)
        {
            if (ModelState.IsValid)
            {
                  model.Task.Add(task);
            var result = model.SaveChanges();
            if (result>0)
            {
                return RedirectToAction("Index");
            }
            }
            return View(task);
        }
       
        //查看详情
        public ActionResult Details(int id)
        {
            Task task = model.Task.FirstOrDefault(p=>p.TaskID==id);
            return View(task);
        }

        //删除
        [HttpPost]
        public ActionResult Delect(int id)
        {
            Task task = model.Task.FirstOrDefault(p=>p.TaskID==id);
            if (task!=null)
            {
                model.Task.Remove(task);
                var result = model.SaveChanges() > 0;
                return Json(result);
            }
            return Json(false);
        }

        public ActionResult Edit(int id)
        {
            var task = model.Task.FirstOrDefault(p => p.TaskID == id);
            return View(task);
        }
        [HttpPost]
        public ActionResult Edit(Task task)
        {
            if (ModelState.IsValid)
            {
                var list = model.Task.Where(p => p.TaskID == task.TaskID).FirstOrDefault();
                if (list != null)
                {
                    list.TaskID = task.TaskID;
                    list.TaskPriority = task.TaskPriority;
                    list.Content = task.Content;
                    list.AddTime = task.AddTime;
                    list.TaskState = task.TaskState;
                    list.FinishedTime = task.FinishedTime;
                    list.uid = task.uid;
                    var u = model.SaveChanges();
                    if (u > 0)
                    {
                        return RedirectToAction("Index");

                    }
                }
            }
            return View(task);

        }

    }
    
    }